<template>
  <div class="about">
    <Dialog v-model="visible" :title="'配置列表展示字段'" class="notice-dialog1">
      <template #header="{ close, titleId, titleClass }">
        <div class="my-header">
          <h4 :id="titleId" :class="titleClass">
            <span>配置列表展示字段 </span>
            <span class="spanTip"> (用户可拖动排序)</span>
          </h4>
          <el-icon @click="close" class="el-icon--left">
            <CircleClose />
          </el-icon>
        </div>
      </template>
      <Draggable
        :list="dragArr"
        ghost-class="ghost"
        :force-fallback="true"
        chosen-class="chosenClass"
        animation="300"
        @start="onDragStart"
        @end="onDragEnd"
        :sort="true"
        handle=".drag"
        item-key="id"
      >
        <template #item="{ element, index }">
          <div :class="element.state == 1 ? 'item1 item' : 'item'">
            <i><el-checkbox
                @change="checkoutChange(element, index)"
                v-model="element.state"
                size="large"
                :label="element.label"
                :true-label="1"
                :false-label="0"
              />
            </i>
            <i>{{ index + 1 }}</i>
            <el-button link class="drag">拖动</el-button>
          </div>
        </template>
      </Draggable>
      <template #footer>
        <span class="dialog-footer">
          <el-button class="dialogCancelBtn" @click="confirm" style="min-width: 80px"
            >取 消</el-button
          >
          <el-button class="dialogOkBtn" type="primary" @click="submitDrag" style="min-width: 80px">
            确 定
          </el-button>
        </span>
      </template>
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import Draggable from 'vuedraggable'

const props = defineProps({
  dragDialog: {
    type: Boolean,
    required: true
  },
  dragArr: {
    type: Array
  }
})

const emit = defineEmits([
  'update:dragDialog',
  'submitDrag',
  'checkoutChange',
  'onDragStart',
  'onDragEnd'
])
watch(
  () => props.dragDialog,
  () => {
    visible.value = props.dragDialog
  }
)
const visible = ref(false)
// 关闭
const confirm = () => {
  emit('update:dragDialog', false)
}
// 提交
const submitDrag = () => {
  emit('submitDrag')
}
// 复选框勾选
const checkoutChange = (element, index) => {
  console.log(1111, props.dragArr)
  emit('checkoutChange', element, index)
}
// 拖动前
const onDragStart = () => {
  emit('onDragStart')
}
// 拖动后
const onDragEnd = () => {
  emit('onDragEnd')
}
</script>
<style lang="scss" scoped>
.notice-dialog1 {
  padding: 0 12px;
  border-radius: 15px;
  .my-header {
    position: relative;
    h4 {
      text-align: center;
      height: 30px;
    }
    .el-icon {
      position: absolute;
      top: 2px;
      right: -10px;
      cursor: pointer;
    }
    .spanTip {
      font-size: 14px;
      color: #999;
    }
  }

  .item {
    padding: 6px 10px;
    width: 46%;
    display: inline-flex;
    justify-content: space-between;
    margin: 0 0.5%;
    // background-color: #f6f8fa;
    background-color: var(--el-card-bg-color);
    border-radius: 5px;
    line-height: 42px;

    i:nth-child(1) {
      width: 70%;
      height: 42px;
      line-height: 46px;
    }
    i:nth-child(2) {
      width: 12%;
      height: 16px;
      line-height: 16px;
      margin: auto 0;
      text-align: center;
      text-decoration: underline;
      border-left: 1px solid #ccc;
      border-right: 1px solid #ccc;
    }
    i:nth-child(3) {
      width: 8%;
      height: 42px;
      line-height: 42px;
      text-decoration: underline;
      padding-left: 20px;
      img {
        width: 15px;
        height: 20px;
        margin-top: 10px;
      }
    }

    .el-checkbox.el-checkbox--large {
      height: 7%;
    }
    .el-checkbox__input.is-checked .el-checkbox__inner {
      background-color: #114ed9;
      border-color: #114ed9;
    }
    .el-checkbox__input.is-checked + .el-checkbox__label {
      color: #114ed9;
    }
  }
  .item1 {
    background-color: #ffffff;
    box-shadow: 0 0 10px #0000001a;
    border-left: solid 2px #114ed9;
    width: 45.8%;
    line-height: 40px;
  }

  .drag {
    cursor: move;
  }

  .item + .item {
    margin-top: 20px;
  }

  .ghost {
    border: solid 1px rgb(19, 41, 239) !important;
  }

  .chosenClass {
    background-color: #eee;
    opacity: 1;
    border: solid 1px red;
  }
}
.notice-dialog1 .el-dialog__header {
  border-bottom: 1px solid #f6f6f6;
  margin-right: 0;
}
.notice-dialog1 .el-dialog__body {
  height: 300px;
  overflow: auto;
}
.notice-dialog1 .el-dialog__body::-webkit-scrollbar {
  display: none;
}
.notice-dialog1 .el-dialog__footer {
  border-top: 1px solid #f6f6f6;
  text-align: center;
  padding-top: 20px;
}
</style>
